#searchclass.py

#get a phrase and split the words
#filter according to the words
def connections(self,win,ui):
		win.connect(ui.action_Quit, SIGNAL('triggered()'), SLOT('close()'))
		self.connect(self.ui.actionAdd_Data, SIGNAL('triggered()'),self.loadNewTable)
		self.connect(self.ui.addTab,SIGNAL('clicked()'),self.newTab)
		self.connect(self.ui.removeTab,SIGNAL('clicked()'),self.delTab)
		self.connect(self.ui.lineEdit,SIGNAL('textChanged(QString)'),self.updateInfo)
		self.connect(self.ui.tabWidget,SIGNAL('currentChanged(int)'),self.tabChangeUpdate)
	
	#some slots
	def newTab(self):
		self.connect(self.ui.newTab(),SIGNAL('anchorClicked(QUrl)'),self.handleLinks)
	
	def delTab(self):
		if self.ui.tabWidget.count()<=1:
			#del the next line to prevent the last tab from being deleted
			self.ui.lineEdit.setText("")
		else:
			self.ui.deleteTab()
	
	def handleLinks(self,url):
		str=QUrl(url).toString()
		self.ui.lineEdit.setText(str)

	
	def tabChangeUpdate(self,x):
		if self.ui.tabWidget.tabText(x)=="PythonGallery":
			self.ui.lineEdit.setText("set to this so that there is surely a change, then set to empty")
			self.ui.lineEdit.setText("")
		else:
			self.ui.lineEdit.setText(self.ui.tabWidget.tabText(x))
	
	def loadNewTable(self):
		filename=QFileDialog.getOpenFileName(None,"Open text file to import to database","/home","Text files( *.txt )")
		return self.table.loadFile(filename)
	
	def updateInfo(self,txt):
		
		if txt=='':
			self.ui.curBrow().setHtml(welcome.welcome_message)
			self.ui.curTabText("PythonGallery")
		else:
			phrase=QString(txt).simplified()
			self.ui.curTabText(phrase)
			phrase=phrase.split(" ")
			phrase[-1]+='%'		#in most cases last word is incomplete
			self.table.filterWords(phrase)
			itemNum=self.table.rowCount()
			if itemNum==0:
				src="No items found. Sorry. Try importing another database"
			elif itemNum==1:
				src=htmlLayout(self.table.getRowData(0))
			else:
				src=self.generateHtmlSource(range(3))
			
			browser=self.ui.curBrow()
			browser.clear()
			browser.setHtml(src)
		
	
	
	def generateHtmlSource(self,ls):
		'''this func gets form the table the lines and prints them out one by one'''
		i=0
		src=""
		tmp=self.table.getRowData(i)
		while tmp!=None:
			src+='<a href="'+' '.join([str(tmp[x]) for x in ls])+'">'
			for j in ls:
				src+=tmp[j]+", "
			src+="</a><br/>"
			i+=1
			tmp=self.table.getRowData(i)
		return src
